package com.verify.util;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.SneakyThrows;

import java.util.List;

/**
 * @author Wang Chao
 * @date 2024-04-03 14:51
 * @since 2.0
 */
public class JariJson {
    private static final ObjectMapper mapper;

    static {
        mapper = new ObjectMapper().registerModule(new JavaTimeModule());
    }

    @SneakyThrows
    public static <T> T str2Obj(String json, Class<T> clazz) {
        return mapper.readValue(json, clazz);
    }

    @SneakyThrows
    public static <T> List<T> str2List(String json, Class<T> clazz) {
        JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, clazz);

        return mapper.readValue(json, javaType);
    }

    @SneakyThrows
    public static String obj2str(Object json) {
        return mapper.writeValueAsString(json);
    }
}
